package com.cnzz.site1255174697.activity.taobao;

import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.cnzz.site1255174697.Const;
import com.cnzz.site1255174697.R;
import com.cnzz.site1255174697.base.BaseRecyclerActivity;
import com.cnzz.site1255174697.factory.GoodsFactory;
import com.cnzz.site1255174697.holder.ChoiceListAdapter;
import com.cnzz.site1255174697.holder.GoodsListHolder;
import com.cnzz.site1255174697.model.MallEntity;
import com.cqyanyu.yanyu.model.EventEntity;
import com.cqyanyu.yanyu.view.recyclerView.YLoadMoreHolder;
import com.cqyanyu.yanyu.view.recyclerView.YRecyclerView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

/**
 * 使用商品具体列表
 */
@ContentView(R.layout.activity_kinds_list)
public class KindsListActivity extends BaseRecyclerActivity implements RadioGroup.OnCheckedChangeListener {

    @ViewInject(R.id.fab)
    protected ImageButton fab;
    protected RadioGroup radioGroup;
    protected RadioButton tvMoren;
    private String title, name;//类型
    private String tag = "default";
    private boolean is_tmall = false;//默认淘宝

    @ViewInject(R.id.mRecyclerView)
    private YRecyclerView mRecyclerView;
    private PopupWindow popupWindow;
    private ChoiceListAdapter choiceListAdapter;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    serch("total_sales");
                    break;
                case 2:
                    serch("is_tmall");
                    break;
                case 3:
                    serch("default");
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        title = getIntent().getStringExtra("title");
        name = getIntent().getStringExtra("name");
        setTitle(title);
        mRecyclerView.bindItemHolder(MallEntity.DataBean.ResultsBean.AitaobaoItemBean.class, GoodsListHolder.class);
        mRecyclerView.setSpanCount(2);
        mRecyclerView.setCellSpanCount(YLoadMoreHolder.class, 2);
        initView();
        loadData();
    }

    @Override
    public void loadData() {
        super.loadData();
        JSONArray jsonArray = new JSONArray();
        JSONObject jsonObject1 = new JSONObject();
        JSONObject jsonObject2 = new JSONObject();
        JSONObject jsonObject3 = new JSONObject();
        JSONObject jsonObject4 = new JSONObject();
        JSONObject jsonObject6 = new JSONObject();
        JSONObject jsonObject7 = new JSONObject();
        try {
//            jsonObject1.put("fields", "open_iid,title,nick,pic_url,price,end_volume,commission,commission_rate,commission_num,commission_volume,seller_credit_score,item_location,volume,coupon_price,coupon_rate,coupon_start_time,coupon_end_time,shop_type");
            jsonObject1.put("fields","num_iid,title,pict_url,small_images,reserve_price,zk_final_price,user_type,provcity,item_url,seller_id,volume,nick");
            if (!TextUtils.isEmpty(name)) {
                if (TextUtils.equals(title, "全部")) {
                    if (TextUtils.equals(name, "美妆配饰")) {
                        jsonObject2.put("keyword", "美妆");
                    } else {
                        jsonObject2.put("keyword", name);
                    }
                } else if (!TextUtils.equals(title, "全部") && (TextUtils.equals(name, "家纺/家装") || TextUtils.equals(name, "母婴")
                        || TextUtils.equals(name, "美妆配饰") || TextUtils.equals(name, "居家")
                        || TextUtils.equals(name, "数码家电") || TextUtils.equals(name, "蛋类") || TextUtils.equals(name, "生鲜")
                        || TextUtils.equals(name, "箱包") || TextUtils.equals(title, "茶叶酒水") || TextUtils.equals(name, "运动/户外")
                        || TextUtils.equals(name, "鞋靴"))) {
                    jsonObject2.put("keyword", title);
                } else {
                    jsonObject2.put("keyword", title + name);
                }
            } else {
                jsonObject2.put("keyword", title);
            }
            jsonObject3.put("page_no", page + "");
            if (!TextUtils.isEmpty(tag)) {
                if (tag.equals("is_tmall")){
                    jsonObject7.put("is_tmall",true);
                }else if (tag.equals("default")){
                    jsonObject7.put("is_tmall",false);
                }else{
                    jsonObject4.put("sort", tag);
                }
            }
            jsonObject6.put("page_size", "100");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        jsonArray.put(jsonObject1);
        jsonArray.put(jsonObject2);
        jsonArray.put(jsonObject3);
        jsonArray.put(jsonObject4);
        jsonArray.put(jsonObject6);
        jsonArray.put(jsonObject7);
        GoodsFactory.getGoods(this, "tbk.item.get", jsonArray.toString(), page, this);
    }

    private void initView() {
        mRecyclerView.setOnLoadMoreListener(this);
        mRecyclerView.setOnRefreshListener(this);
        radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
        mRecyclerView = (YRecyclerView) findViewById(R.id.mRecyclerView);
        fab = (ImageButton) findViewById(R.id.fab);
        radioGroup.setOnCheckedChangeListener(this);
        tvMoren = (RadioButton) findViewById(R.id.tv_moren);
    }

    private void serch(String paramString) {
        mRecyclerView.setRefreshing(true);
        tag = paramString;
        loadData();
    }

    @Override
    public void onBtnClick(View v) {
        super.onBtnClick(v);
        switch (v.getId()) {
            case R.id.btn_right:
                startActivity(new Intent(this, SearchActivity.class));
                break;
            case R.id.fab:
                //底部按钮
                mRecyclerView.getRecyclerView().smoothScrollToPosition(0);
                break;
        }
    }

    private void showDialog(View viewGroup) {
        View rootView = LayoutInflater.from(this).inflate(R.layout.dialog_choice, null);
        popupWindow = new PopupWindow(rootView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setOutsideTouchable(true);  //设置点击空白处消失
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.showAsDropDown(viewGroup);//设置位置
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = 0.7f;
        getWindow().setAttributes(lp);
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                WindowManager.LayoutParams lp = getWindow().getAttributes();
                lp.alpha = 1f;
                getWindow().setAttributes(lp);
            }
        });
        popupWindow.setFocusable(true);
        ListView listView = (ListView) rootView.findViewById(R.id.cListView);
        choiceListAdapter = new ChoiceListAdapter(this);
        listView.setAdapter(choiceListAdapter);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(EventEntity eventEntity) {
        if (eventEntity.type == Const.JUMP_CLICK_LIST) {
            popupWindow.dismiss();
            choiceListAdapter.setSelect(eventEntity.id);
            String[] name = (String[]) eventEntity.data;
            tvMoren.setText(name[eventEntity.id]);
            switch (eventEntity.id) {
                case 0:
                    serch("tk_rate");
                    return;
//                case 1:
//                    serch("delistTime_desc");
//                    return;
                case 1:
                    serch("_des");
                    return;
                case 2:
                    serch("_asc");
                    break;
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        switch (i) {
            case R.id.tv_moren:
                //默认
                showDialog(tvMoren);
                break;
            case R.id.tv_xl:
                //销量
                Message message = new Message();
                message.what = 1;
                handler.sendMessage(message);
                break;
            case R.id.tv_tianmao:
                //天猫
                Message message1 = new Message();
                message1.what = 2;
                handler.sendMessage(message1);
                break;
            case R.id.tv_taobao:
                //淘宝
                Message message2 = new Message();
                message2.what = 3;
                handler.sendMessage(message2);
                break;
        }

    }
}
